<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (Schema::hasTable('commerce_commodity')) {
            return;
        }
        Schema::create('commerce_commodity', function (Blueprint $table) {
            $table->comment('商品');
            $table->bigIncrements('id')->comment('主键ID');
            $table->unsignedBigInteger('merchant_id')->unsigned()->comment('商户ID');
            $table->string('commodity_name', 128)->comment('商品名称');
            $table->integer('commodity_category')->comment('商品分类');
            $table->string('commodity_brand', 64)->nullable()->comment('商品品牌');
            $table->string('commodity_model', 32)->nullable()->comment('商品型号');
            $table->json('commodity_spec')->nullable()->comment('商品规格');
            $table->string('commodity_unit', 8)->default('件')->comment('计量单位');
            $table->text('commodity_description')->nullable()->comment('商品描述');
            $table->decimal('sale_price', 10, 2)->comment('销售价');
            $table->decimal('discount', 6, 2)->default(1.00)->comment('折扣');
            $table->decimal('full_amount', 10, 2)->default(0.00)->comment('满减满足金额');
            $table->decimal('full_reduction', 10, 2)->default(0.00)->comment('满减扣除金额');
            $table->integer('stock_quantity')->default(0)->comment('库存数量');
            $table->integer('sales_quantity')->default(0)->comment('销售数量');
            $table->json('commodity_images')->nullable()->comment('商品图片（JSON格式）');
            $table->unsignedTinyInteger('status')->default(1)->comment('商品状态：1-上架，2-下架');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->text('remark')->nullable()->comment('备注');
            $table->index('merchant_id', 'idx_merchant_id');
            $table->index('commodity_name', 'idx_commodity_name');
            $table->index('commodity_category', 'idx_commodity_category');
            $table->index('status', 'idx_status');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('commerce_commodity');
    }
};